import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from "views/login/Login"


const Login = () =>
    import ('views/login/Login')
const Home = () =>
    import ('views/home/Home')

const WelCome = () =>
    import ('views/home/welcome/WelCome')
const User = () =>
    import ('views/user/User')
const Rights = () =>
    import ('views/power/Rights')
const Roles = () =>
    import ('views/power/Roles')
const Cate = () =>
    import ('views/goods/Cate')
const Params = () =>
    import ('views/goods/Params')
const GoodsList = () =>
    import ('views/goods/List')
const GoodsAdd = () =>
    import ('views/goods/Add')
const Order = () =>
    import ("views/order/Order")
const Report = () =>
    import ("views/report/Report")
Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/login'
}, {
    path: '/login',
    component: Login
}, {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [{
        path: '/welcome',
        component: WelCome
    }, {
        path: '/users',
        component: User
    }, {
        path: '/rights',
        component: Rights
    }, {
        path: '/roles',
        component: Roles
    }, {
        path: '/categories',
        component: Cate
    }, {
        path: '/params',
        component: Params
    }, {
        path: '/goods',
        component: GoodsList
    }, {
        path: '/goods/add',
        component: GoodsAdd
    }, {
        path: '/orders',
        component: Order
    }, {
        path: '/reports',
        component: Report
    }]
}]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
    if (to.path === '/login') return next()
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
    next()
})

export default router